var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var app = getApp();
var sourceType = [
    ['camera'],
    ['album'],
    ['camera', 'album']
]
var sizeType = [
    ['compressed'],
    ['original'],
    ['compressed', 'original']
]
Page(common.extendPage(PageBase, {
    data: {
        winHei: 0,
        dataLoaded: false,
        shopInfo: {},
        shopLogo: '',
        onOff: true,
        submiting: false,
        category: [],
        floor: [],
        imgListArr: [],
        sourceTypeIndex: 2,
        sourceType: ['拍照', '相册', '拍照或相册'],
        sizeTypeIndex: 2,
        sizeType: ['压缩', '原图', '压缩或原图'],
        countIndex: 8,
        count: [1, 2, 3, 4, 5, 6, 7, 8, 9],
        currentFloor: '',
        currentCategory: '',
        categoryId: '',
        floorId: ''
    },

    onLoad: function() {
        this._getShopInfo();
    },
    /**
     * 获取店铺信息
     */
    _getShopInfo: function() {
        var category = this.data.category;
        var floor = this.data.floor;
        var imgListArr = this.data.imgListArr;
        var currentFloor = this.data.currentFloor;
        var currentCategory = this.data.currentCategory;
        var url = "/market/Shop/info";
        var data = {};

        ajax.P({
            url,
            data,
            success: (data) => {

                var shopInfo = data.result.info;

                shopInfo['category'].forEach((item) => {
                    category.push(item.cate_name);
                    if (item.selected == 1) {
                        currentCategory = item.cate_name;
                    }
                })
                shopInfo['floor'].forEach((item) => {
                    floor.push(item.title);
                    if (item.selected == 1) {
                        currentFloor = item.title;
                    }
                })

                if (shopInfo['image_urls']) {
                    imgListArr = shopInfo['image_urls'].split('|');
                    imgListArr.some((item, index) => {
                        if (!item) {
                            imgListArr.splice(index, 1);
                        }
                    })
                }


                this.setData({
                    shopInfo,
                    category,
                    floor,
                    imgListArr,
                    dataLoaded: true,
                    currentCategory,
                    currentFloor,
                    shopLogo: shopInfo.logo,
                    categoryId: shopInfo.category_id,
                    floorId: shopInfo.floor_id
                })
            }
        })
    },
    /**
     * 编辑/保存
     */
    _edit: function(evt) {

        var onOff = this.data.onOff;
        var url = "/market/Shop/edit";
        var shopLogo = this.data.shopLogo;
        var imgListArr = this.data.imgListArr;
        var floorId = this.data.floorId;
        var categoryId = this.data.categoryId;

        var {
            shopName,
            shopAddr,
            synopsis
        } = evt.detail.value;
        var imgListStr = imgListArr.join('|');
        var data = {
            title: shopName,
            logo: shopLogo,
            synopsis: synopsis,
            addr: shopAddr,
            floor_id: floorId,
            category_id: categoryId,
            image_urls: imgListStr
        }

        this.setData({
            onOff: false
        })

        if (!onOff) {
            this.setData({
                submiting: true
            })
            ajax.P({
                url,
                data,
                success: (data) => {
                    util.alert(data.msg);
                    this.setData({
                        onOff: true,
                        submiting: false
                    })
                },
                fail: (data) => {
                    this.setData({
                        submiting: false
                    })
                }
            })
        }


    },
    /**
     * 楼层切换
     */
    _floorPickerChange: function(evt) {
        var floorIndex = evt.detail.value;
        var floor = this.data.floor;
        var shopInfo = this.data.shopInfo;

        this.setData({
            currentFloor: floor[floorIndex],
            floorId: shopInfo.floor[floorIndex].id
        })
    },
    /**
     * 分类切换
     */
    _catePickerChange: function(evt) {
        var cateIndex = evt.detail.value;
        var category = this.data.category;
        var shopInfo = this.data.shopInfo;

        this.setData({
            currentCategory: category[cateIndex],
            categoryId: shopInfo.category[cateIndex].id
        })
    },
    /**
     * 选择图片
     */
    chooseImage: function(evt) {

        var imgTy = evt.currentTarget.dataset.imgTy;
        var imgListArr = this.data.imgListArr;
        var count = this.data.count;
        var onOff = this.data.onOff;

        count = count.length - imgListArr.length;

        if (onOff) {
            return false;
        }

        wx.chooseImage({
            sourceType: sourceType[this.data.sourceTypeIndex],
            sizeType: sizeType[this.data.sizeTypeIndex],
            count: count,
            success: (res) => {
                this.doFileUpload(res.tempFilePaths, imgTy);
            }
        })
    },
    /**
     * 预览图片
     */
    previewImageTG: function(e) {

        var current = e.target.dataset.src;

        wx.previewImage({
            current: current,
            urls: this.data.imgListArr
        })
    },
    /**
     * 上传图片
     */
    doFileUpload: function(arr, imgTy) {

        var imgListArr = this.data.imgListArr;

        arr.forEach((item) => {
            wx.uploadFile({
                url: app.config.apiUrl + '/openapi/UploadImg/upload',
                filePath: item,
                name: 'image',
                //formData:{'user': 'test'},
                header: {
                    'content-type': 'multipart/form-data',
                },
                success: (res) => {
                    var d = res.data;
                    if (typeof(d) == 'string') {
                        d = JSON.parse(d);
                    }
                    if (d.code == 0) {
                        var imgUrl = d.result.url;
                        if (imgUrl) {
                            if (imgTy == 1) {
                                imgListArr.push(imgUrl);
                                this.setData({
                                    imgListArr
                                })
                            } else {
                                this.setData({
                                    shopLogo: imgUrl
                                })
                            }
                        }
                    }
                }
            })
        })
    },
    /**
     * 删除图片
     */
    removerUrl: function(e) {
        var imgListArr = this.data.imgListArr;
        imgListArr.splice(e.currentTarget.dataset.index, 1);
        this.setData({
            imgListArr
        })
    },
    _hint: function() {
        var onOff = this.data.onOff;
        if (!onOff) {
            util.alert('请联系商场管理员修改');
        }
    }
}));